import React, { Component } from "react";

import PageContent from '../PageContent'

import {Badge} from 'antd';

import { ajax ,showResultMessage} from '../common';

import {UIDragDataTableView} from '../uicomponents/UIDataComponentView';
import UIFormView from '../uicomponents/UIFormView';

/** 
 * 系统应用域管理
*/
export default class SysDomainListApp extends Component {
    constructor(props) {
        super(props);

    }

    render() {
        return (
            <PageContent pageInstance={this} {...this.props}>
                <SysDomainListPanel />
            </PageContent>
        );
    }
}

/** 
 * 系统应用域列表
*/
class SysDomainListPanel extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            formTitle: '',
        }
    }

    actionHandler = (actionEvent) => {
        const { selectSysDomainIds, sysDomainListRef, sysDomainFormRef } = this;

        let selectIds = [];

        if (actionEvent.params !== undefined) {
            selectIds.push(actionEvent.params.id);
        }
        else {
            selectIds = selectSysDomainIds;
        }

        return {
            refresh: () => {
                sysDomainListRef.loadData();
            },
            addSysDomain: () => {
                this.setState({ formTitle: '新增系统应用域' }, () => {
                    sysDomainFormRef.openModal();
                });
            },
            editSysDomain: (actionEvent) => {
                this.setState({ formTitle: '编辑系统应用域' }, () => {
                    sysDomainFormRef.openModal(() => {
                        sysDomainFormRef.setFormData(actionEvent.params);
                    });
                });
            },
            doOk: () => {
                sysDomainFormRef.getFormData((formData) => {
                    const result = {};
                    ajax('SYSTEM/APP/saveSysDomain', formData).then(function (data) {
                        if (data) {
                            result.success = true;
                            result.message = 'message.saveSuccess';
                            sysDomainListRef.loadData();
                            sysDomainFormRef.closeModal();
                        }
                    }).catch(function (reason) {
                        result.success = false;
                        result.message = reason;
                    }).finally(() => {
                        showResultMessage(result);
                    });
                });
            },
            closeDialog: () => {
                sysDomainFormRef.closeModal();
            },
            deleteSysDomain: (actionEvent) => {

                ajax('SYSTEM/APP/deleteSysDomain', { 'ids': selectIds }, (data) => {
                    sysDomainListRef.loadData();
                });
            },
            setSysDomainSort : () => {

            },
            moveUp: (actionEvent) => {

                ajax('SYSTEM/APP/moveUpSysDoamin', { 'domainId': selectIds[0] }, (data) => {
                    sysDomainListRef.loadData({}, false);
                });
            },
            moveDown: (actionEvent) => {

                ajax('SYSTEM/APP/moveDownSysDoamin', { 'domainId': selectIds[0] }, (data) => {
                    sysDomainListRef.loadData({}, false);
                });
            }
        };
    }

    render() {

        const sysDomainTableProps = {
            actionHandler: this.actionHandler,
            actionGroupId: "sysDomainListActionGroup",
            rowActionGroupId : "sysDomainRowActionGroup",
            tableConfigId: "sysDomainListTable",
            dataUrl: "SYSTEM/APP/querySysDomainList",
            selectMode: "multi",
            searchFormId: "sysDomainSearchForm",
            autoLoad: true,
            canDrag : true,
            onMoveRow : (dragData,hoverData) => {
                               
                ajax('SYSTEM/APP/setSysDoaminSort', {domainId : dragData.id,sortIndex : hoverData.sort},data => {
                    this.sysDomainListRef.loadData({});
                });
            },
            onSelectChange: (selectRowKeys) => {
                this.selectSysDomainIds = selectRowKeys;
            },
            setToolbarState: (topToolBar, selectedIds) => {
                if (selectedIds.length > 0) {
                    topToolBar.enable('delete');
                    topToolBar.enable('edit');
                    topToolBar.enable('moveUp');
                    topToolBar.enable('moveDown');
                }
                else {
                    topToolBar.disable('delete');
                    topToolBar.disable('edit');
                    topToolBar.disable('moveUp');
                    topToolBar.disable('moveDown');
                }
            },
            cellRender : (column,CellContent,data,dataSet) => {
                if(column.property === 'status'){
                    CellContent.splice(0, 0, <Badge key={data.id} status={data.status === 'IN_USE' ? 'processing' : 'error'} />);
                }
            }
        };

        const sysDomainFormProps = {
            formConfigId: 'sysDomainForm',
            actionGroupId: 'defaultDiaoligActionGroup',
            actionHandler: this.actionHandler,
            inPop: true,
            destroyOnClose: true,
        };

        return (
            <React.Fragment>
                <UIDragDataTableView ref={(inst) => this.sysDomainListRef = inst} {...sysDomainTableProps} />
                <UIFormView title={this.state.formTitle} ref={(inst) => this.sysDomainFormRef = inst} {...sysDomainFormProps} />
            </React.Fragment>
        );
    }
}